package com.youchuang.redistest.common;

import java.io.Serializable;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class RedisCacheTxInterceptor {

	@Autowired
	protected RedisTemplate<Serializable, Serializable> redisTemplate;

	@Pointcut("execution(* com.youchuang.redistest.service.impl.*.*(..))")
	private void theMethod() {
	}

	@Around("theMethod()")
	public Object openRedisTx(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("openRedisTx");
		// Object[] args = pjp.getArgs();
		//
		// redisTemplate.multi();
		// Object obj = pjp.proceed(args);
		//
		// redisTemplate.exec();
		Object obj = pjp.proceed();

		return obj;
	}

	@AfterThrowing(pointcut = "theMethod()", throwing = "e")
	public void rollbackTx(JoinPoint jp, Exception e) {
		if (e != null) {
			System.out.println("执行异常");
		}
		// redisTemplate.discard();
	}
}
